package com.gjy.redis.controller;

import com.gjy.redis.config.Key;
import com.gjy.redis.domain.AjaxResult;
import com.gjy.redis.domain.middle.GeoMember;
import com.gjy.redis.domain.vo.GeoAddRequest;
import com.gjy.redis.domain.vo.GeoHashRequest;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.core.GeoOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-07-13 09:03:05
 */
@RestController
@RequestMapping("/geo")
public class GeoController {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    /*@PostMapping("/search")
    public AjaxResult search(@RequestBody GeoSearchRequest request) {
        GeoOperations<String, Object> geo = redisTemplate.opsForGeo();
        GeoResults<RedisGeoCommands.GeoLocation<Object>> search = geo.search(Key.GEO_PREFIX + "NanJing",
                new Circle(new Point(request.getX(), request.getY()),
                        new Distance(request.getDistance(), Metrics.METERS)));
        String k = Key.GEO_PREFIX + "NanJing";
        GeoResults<RedisGeoCommands.GeoLocation<Object>> search2 =
                geo.search(k, GeoReference.fromMember(""), new Distance(3000, Metrics.METERS)
                        , RedisGeoCommands.GeoSearchCommandArgs.newGeoSearchArgs().sort(Sort.Direction.DESC));
        return AjaxResult.builder().data(search).data(search2).build();
    }*/

    @PostMapping("/pos")
    public AjaxResult pos(@RequestBody GeoHashRequest request) {
        GeoOperations<String, Object> geo = redisTemplate.opsForGeo();
        List<Point> position = geo.position(request.getKey(), request.getMembers().toArray());
        return AjaxResult.builder().data(position).build();
    }

    @PostMapping("/hash")
    public AjaxResult hash(@RequestBody GeoHashRequest request) {
        GeoOperations<String, Object> geo = redisTemplate.opsForGeo();
        List<String> hash = geo.hash(request.getKey(), request.getMembers().toArray());
        return AjaxResult.builder().data(hash).build();
    }

    /*@PostMapping("/dist")
    public AjaxResult dist(@RequestBody GeoDistRequest request) {
        GeoOperations<String, Object> geo = redisTemplate.opsForGeo();
        Distance distance = geo.distance(request.getKey(), request.getM1(), request.getM2(), Metrics.METERS);

        return AjaxResult.builder()
                .data(distance)
                .build();
    }*/

    @PostMapping("/add")
    public AjaxResult add(@RequestBody GeoAddRequest request) {
        GeoOperations<String, Object> geo = redisTemplate.opsForGeo();
        Long add = geo.add(Key.GEO_PREFIX + request.getCategory(),
                new Point(request.getGeoMember().getLongitude(), request.getGeoMember().getLatitude()),
                request.getGeoMember().getMember());

        return AjaxResult.builder()
                .code(200)
                .message("添加成功")
                .data(add)
                .build();
    }

    @PostMapping("/add1")
    public AjaxResult add1(@RequestBody List<GeoMember> geoMember) {
        GeoOperations<String, Object> geo = redisTemplate.opsForGeo();
        for (GeoMember member : geoMember) {
            geo.add(Key.GEO_PREFIX + "NanJing", new Point(member.getLongitude(), member.getLatitude()),
                    member.getMember());
        }

        return AjaxResult.builder()
                .code(200)
                .message("添加成功")
                .build();
    }

    @PostMapping("/adds")
    public AjaxResult adds(@RequestBody List<GeoAddRequest> requests) {
        GeoOperations<String, Object> geo = redisTemplate.opsForGeo();
        requests.forEach(request -> geo.add(Key.GEO_PREFIX + request.getCategory(),
                new Point(request.getGeoMember().getLongitude(), request.getGeoMember().getLatitude()),
                request.getGeoMember().getMember()));

        return AjaxResult.builder()
                .code(200)
                .message("添加成功")
                .build();
    }
}
